﻿using FyCaches.Core.CommandHandlers;
using FyCaches.Models;
using FySystem.IO;
using FySystem.IOTFlow;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FyCaches.Core.Services
{
    internal class CacheFlowService
    {
        /// <summary>
        /// 10个缓存库
        /// </summary>
        internal static WorkFlow<CommandInfo>[] CacheFlows = new WorkFlow<CommandInfo>[10];

        /// <summary>
        /// 缓存数组
        /// </summary>
        internal static Dictionary<string, byte[]>[] Caches = new Dictionary<string, byte[]>[10];

        /// <summary>
        /// 缓存指令处理
        /// </summary>
        /// <param name="command"></param>
        internal static void Handle(CommandInfo command)
        {
            List<byte> response = null;
            try
            {
                if (command.Command[0] == 0x00 && command.Command[1] == 0x00)
                {
                    //设置数据
                    int num = SetDataCommand.Handle(command);
                    response = PackUtils.CreatePackByNumber(command, num);
                }
                else if (command.Command[0] == 0x00 && command.Command[1] == 0x01)
                {
                    //查询数据
                    byte[] cdata = GetDataCommand.Handle(command);
                    response = PackUtils.CreatePackByByteArrary(command, cdata);
                }
                else if (command.Command[0] == 0x00 && command.Command[1] == 0x02)
                {
                    //判断键值是否存在
                    byte b = IsExistCommand.Handle(command);
                    response = PackUtils.CreatePackByByte(command, b);
                }
                else if(command.Command[0]==0x00 && command.Command[1] == 0x03)
                {
                    //删除指定key的值
                    byte b = DeleteCommand.Handle(command);
                    response = PackUtils.CreatePackByByte(command, b);
                }
                else if(command.Command[0]==0x00 && command.Command[1] == 0x04)
                {
                    int num = SetDataCommand.BatchHandle(command);
                    response = PackUtils.CreatePackByNumber(command, num);
                }
                else if (command.Command[0] == 0xff && command.Command[1] == 0x00)
                {
                    //订阅主题
                    byte b = SubscribeCommand.Handle(command);
                    response = PackUtils.CreatePackByByte(command, b);
                }
                else if (command.Command[0] == 0xff && command.Command[1] == 0x01)
                {
                    //给指定的主题推送消息
                    int num = PublishCommand.Handle(command);
                    response = PackUtils.CreatePackByNumber(command, num);
                }
                else if(command.Command[0]==0xff && command.Command[1] == 0x02)
                {
                    //取消订阅（不需要给客户端返消息）
                    SubscribeCommand.UnSubscribe(command);
                }
                else if(command.Command[0]==0xff && command.Command[1] == 0xff)
                {
                    //获取服务端版本号
                    byte[] ver = Encoding.UTF8.GetBytes("1.00");
                    response = PackUtils.CreatePackByByteArrary(command, ver);
                }
            }
            catch (Exception ex)
            {
                response = PackUtils.CreatePackByException(command.RequestId, ex);
                FyLogContext.Write(ex.Message);
            }

            //将结果返回给客户端
            CommandService.SendToClient(command.SimClient, response);
        }
    }
}
